package com.mflex.mwms.activity;

import android.os.Bundle;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

import com.mflex.mwms.R;
import com.mflex.mwms.model.CustomApiResult;
import com.mflex.mwms.model.ShelfOnLocation;
import com.mflex.mwms.model.ShelfOnRequest;
import com.mflex.mwms.utils.MMKVUtils;
import com.mflex.mwms.utils.MessageUtils;
import com.mflex.mwms.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.CallBackProxy;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.CollectionUtils;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 入库上架
 */
public class PackWareHouseActivity extends BaseActivity {
    @BindView(R.id.stv_BoxNumber)
    EditText stvBoxNumber;

    @BindView(R.id.stv_RecommendedLocation)
    EditText stvRecommendedLocation;

    @BindView(R.id.stv_ShelfLocation)
    EditText stvShelfLocation;

    MiniLoadingDialog mMiniLoadingDialog;

    List<String> shelfLocationList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pack_ware_house);
        ButterKnife.bind(this);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");
        stvRecommendedLocation.setInputType(InputType.TYPE_NULL);

        shelfLocationList = new ArrayList<>();
        stvBoxNumber.setOnKeyListener(this);
        stvShelfLocation.setOnKeyListener(this);

    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
            switch (view.getId()) {
                case R.id.stv_BoxNumber:
                    checkBoxNumber(view);
                    break;
                case R.id.stv_ShelfLocation:
                    checkShelfLocation(view);
                    break;
                default:
                    break;
            }
        }
        return false;
    }

    private void checkBoxNumber(View v) {
//        CheckShelfOnRequest checkShelfOnRequest = new CheckShelfOnRequest();
//        checkShelfOnRequest.setBoxNumber(stvBoxNumber.getText().toString());
//        checkShelfOnRequest.setCheckAction(0);
//        checkShelfOnRequest.setWarehouse(MMKVUtils.getString("Warehose", ""));

        mMiniLoadingDialog.show();
        XHttp.get("api/ShelfOn/CheckShelfOn")
                .params("BoxNumber", stvBoxNumber.getText().toString())
                .params("Warehouse", MMKVUtils.getString("Warehose", ""))
                .params("checkAction", 1)
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<ShelfOnLocation>, ShelfOnLocation>(new SimpleCallBack<ShelfOnLocation>() {

                    @Override
                    public void onSuccess(ShelfOnLocation response) {
                        mMiniLoadingDialog.dismiss();

                        shelfLocationList = response.getShelfLocations();

                        stvRecommendedLocation.setText(response.getRecommendedLocation());

                        stvShelfLocation.requestFocus();

                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        stvBoxNumber.setText("");
                        stvRecommendedLocation.setText("");
                        stvShelfLocation.setText("");
                        stvBoxNumber.requestFocus();
//                        if (e.getCode() == 100) {
//                            new MaterialDialog.Builder(v.getContext())
//                                    .iconRes(R.drawable.icon_warning)
//                                    .title(R.string.tip_warning)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
//                        if (e.getCode() == 200) {
//                            new MaterialDialog.Builder(v.getContext())
//                                    .iconRes(R.drawable.icon_tip)
//                                    .title(R.string.tip_infos)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
                        MessageUtils.errorMsgAndVoice(PackWareHouseActivity.this, e);
                    }
                }) {
                });
    }

    private void checkShelfLocation(View v) {
        if (StringUtils.isEmpty(stvBoxNumber.getText().toString())) {
            XToastUtils.error("箱包号未输入");
            return;
        }

        if (CollectionUtils.isEmpty(shelfLocationList)) {
            new MaterialDialog.Builder(v.getContext())
                    .iconRes(R.drawable.icon_warning)
                    .title(R.string.tip_warning)
                    .content("没有可用库位")
                    .positiveText(R.string.lab_submit)
                    .show();
            return;
        }

        if (!shelfLocationList.contains(stvShelfLocation.getText().toString())) {
            new MaterialDialog.Builder(v.getContext())
                    .iconRes(R.drawable.icon_warning)
                    .title(R.string.tip_warning)
                    .content(stvShelfLocation.getText().toString() + "不是可用库位")
                    .positiveText(R.string.lab_submit)
                    .show();
            return;
        }
        ShelfOnRequest shelfOnRequest = new ShelfOnRequest();
        shelfOnRequest.setBoxNumber(stvBoxNumber.getText().toString());
        shelfOnRequest.setShelfLocation(stvShelfLocation.getText().toString());
        shelfOnRequest.setWarehouse(MMKVUtils.getString("Warehose", ""));

        mMiniLoadingDialog.show();
        XHttp.post("api/ShelfOn/ShelfOn")
                .upJson(JsonUtil.toJson(shelfOnRequest))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {

                    @Override
                    public void onSuccess(String response) {
                        mMiniLoadingDialog.dismiss();
                        XToastUtils.success("入库成功", 2500);
                        stvBoxNumber.setText("");
                        stvRecommendedLocation.setText("");
                        stvShelfLocation.setText("");
                        stvBoxNumber.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                stvBoxNumber.requestFocus();
                            }
                        }, 200);
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
//                        if (e.getCode() == 100) {
//                            new MaterialDialog.Builder(v.getContext())
//                                    .iconRes(R.drawable.icon_warning)
//                                    .title(R.string.tip_warning)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
//                        if (e.getCode() == 200) {
//                            new MaterialDialog.Builder(v.getContext())
//                                    .iconRes(R.drawable.icon_tip)
//                                    .title(R.string.tip_infos)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
                        MessageUtils.errorMsgAndVoice(PackWareHouseActivity.this, e);
                    }
                }) {
                });
    }


    @Override
    public void onClick(View view) {

    }
}